/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.plian.system.controller.sys.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.config.Resubmit;
import com.plian.system.dto.sys.SystemRecordDTO;
import com.plian.system.entity.sys.SystemRecord;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.sys.system.ISystemRecordService;
import com.plian.system.vo.report.ReportReturnVO;
import com.plian.system.vo.sys.SystemRecordVO;
import com.plian.system.wrapper.sys.SystemRecordWrapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 控制器
 *
 * @author
 * @since 2019-09-27
 */
@RestController
@AllArgsConstructor
@RequestMapping("api/systemrecord")
public class SystemRecordController extends BaseController {

	private ISystemRecordService systemService;

	private SystemRecordWrapper systemRecordWrapper;

	/**
	 * 详情
	 */
	@PostMapping("/detail")
	public R<SystemRecordVO> detail(@RequestBody SystemRecord systemRecord) {
		SystemRecordVO systemVO = systemRecordWrapper.entityToVO(systemService.getOne(Condition.getQueryWrapper(systemRecord)));
		R<SystemRecordVO> r = new R<>(CommonCode.SUCCESS);
		r.setPageData(systemVO);
		return r;
	}

	/**
	 * 分页
	 */
	@PostMapping("/list")
	public ListResult<SystemRecordVO> list(@RequestBody Query query) {
		List<SearchEntity> entityList = query.getSearch();
		if (query.getAscs()==null&&query.getDescs()==null){
			query.setDescs("create_time");
		}
		QueryWrapper<SystemRecord> queryWrapper = SearchParamEntiy.getQueryWrapper(
				Condition.getQueryWrapper(new SystemRecord()), entityList
		);
		TokenSearch<SystemRecord> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenQuery(queryWrapper, query);

		IPage<SystemRecord> pages = systemService.page(Condition.getPage(query), queryWrapper);
		List<SystemRecordVO> systemVOS = systemRecordWrapper.entityToVO(pages.getRecords());

		ListResult<SystemRecordVO> listResult = new ListResult<>(CommonCode.SUCCESS);
		listResult.setList(systemVOS);
		listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
		return listResult;

	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@Resubmit(delaySeconds = 0)
	@LogOperation("国资委制度-提交")
	public R submit(@Valid @RequestBody SystemRecord systemRecord) {
		return systemService.saveOrUpdate(systemRecord) ?
				new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@LogOperation("国资委制度-删除")
	public R remove(@RequestBody PageData pd) {
		List<String> ids = (List<String>)pd.get("ids");
		return systemService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
				? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
	}


	@GetMapping("/failure/{id}")
	public R failure(@PathVariable Long id, String failureTime){
		return systemService.failure(id, failureTime);
	}

    /**
     * 制度 统计接口
     * @param systemRecordDTO
     * @return
     */
    @PostMapping("/summaryList")
    public R<ReportReturnVO> summaryList(@RequestBody SystemRecordDTO systemRecordDTO){
        ReportReturnVO reportReturnVO = systemService.summaryList(systemRecordDTO);
        R<ReportReturnVO> result = new R<>(CommonCode.SUCCESS);
        result.setPageData(reportReturnVO);
        return result;
    }
}
